var map, map2, map3, map4;
var markers = [];
var markers2 = [];
var markers3 = [];
var markers4 = [];

var mcOptions = {
	gridSize : 150,
	maxZoom : 10,
	styles : [
			{
				height : 53,
				url : "http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/images/m1.png",
				width : 53
			},
			{
				height : 56,
				url : "http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/images/m2.png",
				width : 56
			},
			{
				height : 66,
				url : "http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/images/m3.png",
				width : 66
			},
			{
				height : 78,
				url : "http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/images/m4.png",
				width : 78
			},
			{
				height : 90,
				url : "http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/images/m5.png",
				width : 90
			} ]
};

var latlng = new google.maps.LatLng(39.000409, 35.201554);

var myOptions = {
	zoom : 6,
	center : latlng,
	mapTypeId : google.maps.MapTypeId.ROADMAP
};

function initialize() {

	ForUrgentMap();
	ForMap2();
	ForMap3();
	ForMap4();

}

function ForUrgentMap() {

	map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

	var clusterer = new MarkerClusterer(map, markers, mcOptions);

	// GetDatas
	$
			.ajax({
				url : 'http://localhost:8080/EBlood/services/informativeMessage/getAllUrgentMessages',
				dataType : 'json',
				success : function(JSON) {
					$
							.each(
									JSON,
									function(i, coor) {

										var location = new google.maps.LatLng(
												coor.InstitutionJson.Latitude,
												coor.InstitutionJson.Longitude);
										var location2 = new google.maps.LatLng(
												coor.InstitutionJson.Latitude,
												coor.InstitutionJson.Longitude);

										var image = new google.maps.MarkerImage(
												'/EBlood/WEB/images/addPin.png',
												null);
										var image2 = new google.maps.MarkerImage(
												'/EBlood/WEB/images/hospitalMarker.png',
												null);

										var contentStringForUrgent = "";
										var contentStringForInstitution = "";

										// For UrgenPersonMessages
										$
												.each(
														coor.forUrgentPersons,
														function(i, messages) {
															contentStringForUrgent = contentStringForUrgent
																	+ '<div class="mesaj"> <marquee align="middle" scrollamount="1" '
																	+ ' height="100%" width="100%" direction="up"scrolldelay="1">'
																	+ '<b>Bilgilendirici Mesaj : '
																	+ messages.InformativeTitle
																	+ '</b><br/>'
																	+ 'Kurum adi : '
																	+ coor.InstitutionJson.Name
																	+ '<br/>'
																	+ 'Kurum ID : '
																	+ coor.InstitutionJson.ID
																	+ '<br/>'
																	+ 'Bilgilendirici Mesaj : '
																	+ messages.InformativeMessage
																	+ '<br/>------------<br/>'
																	+ '</marquee><br/></div>';

														});

										// For InstitutionMessages
										$
												.each(
														coor.forInstitution,
														function(i, messages) {
															contentStringForInstitution = contentStringForInstitution
																	+ '<div class="mesaj"> <marquee align="middle" scrollamount="1" '
																	+ ' height="100%" width="100%" direction="up"scrolldelay="1">'
																	+ '<b>Bilgilendirici Mesaj : '
																	+ messages.InformativeTitle
																	+ '</b><br/>'
																	+ 'Kurum adi : '
																	+ coor.InstitutionJson.Name
																	+ '<br/>'
																	+ 'Kurum ID : '
																	+ coor.InstitutionJson.ID
																	+ '<br/>'
																	+ 'Bilgilendirici Mesaj : '
																	+ messages.InformativeMessage
																	+ '<br/>------------<br/>'
																	+ '</marquee><br/></div>';

														});

										var markerForInstitution = null;
										var markerForUrgent = null;

										// markers.push(markerForUrgent);
										// markers.push(markerForInstitution);

										if (coor.forInstitution != 0) {
											markerForInstitution = new google.maps.Marker(
													{
														position : location2,
														icon : image2,
														title : coor.InstitutionJson.Name
													// map:map
													});
											var infowindow2 = new google.maps.InfoWindow(
													{
														content : contentStringForInstitution
													});

											google.maps.event
													.addListener(
															markerForInstitution,
															'click',
															function() {
																infowindow2
																		.open(
																				map,
																				markerForInstitution);
															});

											clusterer.addMarker(
													markerForInstitution, true);
										}
										if (coor.forUrgentPersons != 0) {
											markerForUrgent = new google.maps.Marker(
													{
														position : location,
														icon : image,
														title : coor.InstitutionJson.Name
													// map:map
													});

											var infowindow = new google.maps.InfoWindow(
													{
														content : contentStringForUrgent
													});

											google.maps.event
													.addListener(
															markerForUrgent,
															'click',
															function() {
																infowindow
																		.open(
																				map,
																				markerForUrgent);
															});

											clusterer.addMarker(
													markerForUrgent, true);
										}

									});
				}
			});

}

function ForMap2() {

	map2 = new google.maps.Map(document.getElementById("map_canvas2"),
			myOptions);

	var clusterer = new MarkerClusterer(map2, markers2, mcOptions);

	// GetDatas
	$
			.ajax({
				url : 'http://localhost:8080/EBlood/services/institutionDonationInfo/getBloodDonationCenters',
				dataType : 'json',
				success : function(JSON) {

					$
							.each(
									JSON,
									function(i, INS) {

										var location = new google.maps.LatLng(
												INS.Latitude, INS.Longitude);
										var image = new google.maps.MarkerImage(
												'/EBlood/WEB/images/hospitalMarker.png',
												null);

										var contentString = "";
										contentString += "ID : " + INS.ID
												+ " Name " + INS.Name+" <br/>Street Name "+INS.adressJson.StreetName;

										var markerForInstitution = markerForInstitution = new google.maps.Marker(
												{
													position : location,
													icon : image,
													title : INS.Name
												// map:map
												});

										var infowindow = new google.maps.InfoWindow(
												{
													content : contentString
												});

										google.maps.event
												.addListener(
														markerForInstitution,
														'click',
														function() {
															infowindow
																	.open(map2,
																			markerForInstitution);
														});

										clusterer.addMarker(
												markerForInstitution, true);

									});
				}
			});
}

function ForMap3() {
	map3 = new google.maps.Map(document.getElementById("map_canvas3"),
			myOptions);
	var clusterer = new MarkerClusterer(map3, markers3, mcOptions);

	// GetDatas
	$
			.ajax({
				url : 'http://localhost:8080/EBlood/services/institutionDonationInfo/getBloodSuppliers',
				dataType : 'json',
				success : function(JSON) {

					$
							.each(
									JSON,
									function(i, INS) {

										var location = new google.maps.LatLng(
												INS.Latitude, INS.Longitude);
										var image = new google.maps.MarkerImage(
												'/EBlood/WEB/images/hospitalMarker.png',
												null);

										var contentString = "";
										contentString += "ID : " + INS.ID
												+ " Name " + INS.Name+" <br/>Street Name "+INS.adressJson.StreetName;

										var markerForInstitution = null;

										markerForInstitution = new google.maps.Marker(
												{
													position : location,
													icon : image,
													title : INS.Name
												// map:map
												});

										var infowindow = new google.maps.InfoWindow(
												{
													content : contentString
												});

										google.maps.event
												.addListener(
														markerForInstitution,
														'click',
														function() {
															infowindow
																	.open(map3,
																			markerForInstitution);
														});

										clusterer.addMarker(
												markerForInstitution, true);

									});
				}
			});
}
function ForMap4() {
	map4 = new google.maps.Map(document.getElementById("map_canvas4"),
			myOptions);

	var clusterer = new MarkerClusterer(map4, markers4, mcOptions);

	// GetDatas
	$
			.ajax({
				url : 'http://localhost:8080/EBlood/services/institutionDonationInfo/getFinancialDonationCenters',
				dataType : 'json',
				success : function(JSON) {

					$
							.each(
									JSON,
									function(i, INS) {

										var location = new google.maps.LatLng(
												INS.Latitude, INS.Longitude);
										var image = new google.maps.MarkerImage(
												'/EBlood/WEB/images/hospitalMarker.png',
												null);

										var contentString = "";
										contentString += "ID : " + INS.ID
												+ " Name " + INS.Name+" <br/>Street Name "+INS.adressJson.StreetName;

										var markerForInstitution = null;

										markerForInstitution = new google.maps.Marker(
												{
													position : location,
													icon : image,
													title : INS.Name
												// map:map
												});

										var infowindow = new google.maps.InfoWindow(
												{
													content : contentString
												});

										google.maps.event
												.addListener(
														markerForInstitution,
														'click',
														function() {
															infowindow
																	.open(map4,
																			markerForInstitution);
														});

										clusterer.addMarker(
												markerForInstitution, true);

									});
				}
			});
}
